package controller;

import dao.RegisterDao;
import dao.StudentLoginDao;
import dao.impl.RegisterDaoimpl;
import dao.impl.StudentLoginDaoImpl;
import entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Base64;

@WebServlet(urlPatterns = "/RegisterController")
public class RegisterController extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String op=req.getParameter("op");
        HttpSession session=req.getSession(true);
        RegisterDao dao=new RegisterDaoimpl();
        if ("Register".equals(op)) {
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            String full_name = req.getParameter("full_name");
            String role = req.getParameter("userType");
            String sex = req.getParameter("sex");
            String facePhoto = req.getParameter("facePhoto"); // 获取Base64格式的照片数据

            // 将Base64字符串转换为图片并保存到服务器
            // 修改Base64解码和图片保存部分
            String base64Image = facePhoto.split(",")[1];  // 去掉Base64前缀
            byte[] imageBytes = Base64.getDecoder().decode(base64Image);

            // 确保保存目录存在
            File dir = new File("D:/zhuce/");
            if (!dir.exists()) {
                dir.mkdirs();
            }

            // 保存图片
            String imagePath = "uploaded_image_" + username + ".png";
            File file = new File(dir, imagePath);
            try (FileOutputStream fos = new FileOutputStream(file)) {
                fos.write(imageBytes);
                System.out.println("图片保存路径: " + file.getAbsolutePath());
            }

            PrintWriter out = resp.getWriter();
            try {
                // 调用注册方法，添加facePhoto参数
                String Reg = String.valueOf(dao.Register(username, password, full_name, role, sex, imagePath));
                
                if(role.equals("student")){
                    // 学生注册时也保存照片
                    dao.studentAdd(username, sex, imagePath);
                }
                
                if (Reg == null){
                    resp.sendRedirect("denglu/enroll.jsp");
                } else {
                    session.setAttribute("facePhone",facePhoto);
                    resp.sendRedirect("denglu/login.jsp");
                }
            } catch (SQLException | ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }


    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
